package com.prashantdctm.paidemail;



import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.gae.GAEUtils;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;
import com.prashantdctm.utils.EmailUtil;
import com.prashantdctm.utils.HttpUtil;


public class OnePercentServlet extends GaeHttpServlet {
	public static void main(String[] args) throws Exception {
		OnePercentServlet p = new OnePercentServlet();webClient = HttpUtil.getWebClient();
		//start("pkjsp", "rintu123");
		start("poonamd", "poonamd");
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		super.doGet(req, resp);
		CookieManager cm = webClient.getCookieManager();
		CookieManager tmpCM = new CookieManager();
		for(Cookie cookie : cm.getCookies()){
			tmpCM.addCookie(cookie);
		}
		webClient.setCookieManager(tmpCM);
		/*if(!isCron) {return;}
		
		stopWatch.reset();
		stopWatch.start();
		if(req.getParameter("user")!=null) {
			customLog("User = "+req.getParameter("user"));
		}
		
		try {
			start("pkjsp", "rintu123");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		stopWatch.stop();
		customLog("Time taken: " + stopWatch.toString());
		resp.getWriter().println(messageLog.toString());
		if (GAEUtils.isGaeMode()) {
			EmailUtil.sendEmail(messageLog.toString(), "");
		}*/
	}

	public static void start(String username, String pwd) throws Exception {
		customLog("Before logging in onePercent");
		HtmlPage page = null;
		
		try {
			// Get the first page (login), fill form and submit 
		    page = webClient.getPage("http://tokes.1percent.com/main.phtml?page=rewards_home&");
		    HtmlForm form = page.getFormByName("form");
		    
		    form.getInputByName("login_user_id").setValueAttribute(username);         
		    form.getInputByName("login_password").setValueAttribute(pwd); 
		    page = (HtmlPage) form.getInputByValue("Submit").click(); 
		    //dumpPage(page);
		    webClient.waitForBackgroundJavaScript(5000);
	        
	        
		    HtmlAnchor anchor = page.getAnchorByHref("/main.phtml?page=site_activities&");
			page = anchor.click();
			//dumpPage(page);
			
			List <HtmlElement> elements = (List<HtmlElement>) page.getByXPath("//img[@alt = 'Total Tokes']");
			for(HtmlElement element : elements) {
				customLog("Current Balance:");
				customLog("Total Tokes - "+element.getParentNode().getParentNode().getNextSibling().getFirstChild().getFirstChild().getTextContent());
			}
			
			List<HtmlAnchor> anchors = page.getAnchors();
			for(HtmlAnchor anchor1 : anchors) {
					if(anchor1.getHrefAttribute().contains("action=web_links&")) {
						messagesRead++;
						customLog("Clicking toke task "+anchor1.getFirstChild().getTextContent());
						anchor1.click();
					}
			}
			
			elements = (List<HtmlElement>) page.getByXPath("//img[@alt = 'Total Tokes']");
			for(HtmlElement element : elements) {
				customLog("Current Balance:");
				customLog("Total Tokes - "+element.getParentNode().getParentNode().getNextSibling().getFirstChild().getFirstChild().getTextContent());
			}
			
	        webClient.closeAllWindows();
		}catch (Exception e) {
			customException("Exception in checking messages ", e);
			e.printStackTrace();
		}
		
		customLog("Total messages read: " + messagesRead);
		customLog("Previous Earning: " + earningBefore);
		customLog("Current Earning: " + earningAfter);
		// logout
		customLog("Logging out: " + username);
		
		webClient.closeAllWindows();
	}

}
